﻿/**
 * <b>多文件同步下載</b>
 * <p>
 * 多文件同步下載,下載過程有廣播下載數據
 * 使用舉例:
 * var _loader:GameLoader = new GameLoader(new ApplicationDomain(ApplicationDomain.currentDomain));
 * //添加加載列表
 * _loader.add(url,參數集);
 * _loader.add(...);
 * _loader.add(...);
 * //開始加載
 * _loader.start();
 * //開始監聽
 * _loader.addEventListener(GameLoader.COMPLETE,handleFun);
 * _loader.addEventListener(GameLoader.PROGRESS,handleFun);
 * 
 */
package com.kucooler.common.utils{
	import flash.display.Loader;
	import flash.events.EventDispatcher;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;	
	import flash.utils.Dictionary;	
	
	import com.kucooler.common.inter.Iloader;	
	import com.kucooler.common.event.DataEvent;
	/**
	 * 文件同步下載類
	 * @author lmzd
	 */
	public class GameLoader extends EventDispatcher implements Iloader
	{
		protected var loaderArr:Array = new Array();
		public var fogDomain:ApplicationDomain;
		
		private var progress:uint;
		private var complete:uint;
		private var ioError:String;
		private var timerOut:String;
		private var _loadType:String = 'all';
		
		public static var file:Dictionary = new Dictionary(true);		
		public static var PROGRESS:String = "progress";
		public static var COMPLETE:String = "complete";
		public static var IO_ERROR:String = "ioError";
		public static var TIME_OUT:String = "timeOut";
		
		public function GameLoader(domain:ApplicationDomain)
		{
			this.fogDomain = domain;
			return;
		}
		/**
		 * 增加下載對象
		 * @param	param1	下載鏈接
		 * @param	param2	對象屬性 {id:,name:,type:}
		 */
		public function add(param1:String, param2:Object = ''):void {			
			loaderArr.push({url:param1,param:param2});
		}
		/**
		 * 開始加載
		 * 
		 */
		public function start():void {		
			var _lot_1:Object;			
			if ( loaderArr.length == 0 ) {
				return;
			}							
			_loadType = "all";
			complete = loaderArr.length as uint;
			while (loaderArr.length > 0) {				
				_lot_1  = loaderArr.shift();
				var _lot_url:String = _lot_1.url;
				if ( "type" in _lot_1.param && _lot_1.param.type == 'xml') {				
					loadXML(_lot_url, _lot_1.param);
				}else {
					loadFile(_lot_url, _lot_1.param);								
				}
			}	
		}
		/**
		 * 
		 */
		public function quence():void {
			var _lot_1:Object;			
			if ( loaderArr.length == 0 ) {				
				return;
			}
			_loadType = "quence";
			complete = (complete > 0) ? complete : loaderArr.length as uint;
			_lot_1  = loaderArr.shift();	
			var _lot_url:String = _lot_1.url;
			if ( "type" in _lot_1.param && _lot_1.param.type == 'xml') {				
				loadXML(_lot_url, _lot_1.param);
			}else {
				loadFile(_lot_url, _lot_1.param);								
			}
		}
		/**
		 * 文件(swf,png,gif,jpg,jepg,mp3,mp4.......)下載並開始廣播
		 * @param	param1	文件路徑
		 * @param	param2	文件屬性
		 */
		public function loadFile(param1:String, param2:Object = ''):void {
			var loader:Loader;
			var onComplete:Function;
			var onProgress:Function;
			var onIoError:Function;
			onComplete = function(evt) {				
				file[param2.id] = loader;
				var _com:Object = { name:param2.name, com: --complete};
				dispatchEvent(new DataEvent(COMPLETE, _com));		
				if (_loadType == "quence") quence();
			}
			onProgress = function(evt) {
				var _lot_1:Object;
				_lot_1 = { loaded:evt.bytesLoaded , total:evt.bytesTotal , name:param2.name };				
				dispatchEvent(new DataEvent(PROGRESS,_lot_1));				
			}
			onIoError = function(evt) {		
				dispatchEvent(new DataEvent(IO_ERROR));
			}
			loader = new Loader();			
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);			
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);	
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
			loader.load(new URLRequest(param1), new LoaderContext(true, fogDomain));
		}
		/**
		 * 加載其他的xml文件
		 * @param	param1
		 * @param	param2
		 */
		public function loadXML(param1:String, param2:Object = '' ):void {
			var loader:URLLoader;
			var onComplete:Function;
			var onProgress:Function;
			var onIoError:Function;
			onComplete = function(evt) {
				file[param2.id] = XML(loader.data);
				var _com:Object = { name:param2.name, com: --complete};
				dispatchEvent(new DataEvent(COMPLETE, _com));
				if (_loadType == "quence") quence();
			}
			onProgress = function(evt) {
				return;
			}
			onIoError = function(evt) {
				return;
			}			
			loader = new URLLoader();			
			loader.addEventListener(Event.COMPLETE, onComplete);			
			loader.addEventListener(ProgressEvent.PROGRESS, onProgress);	
			loader.load(new URLRequest(param1));
		}
		/**
		 * 取得文件內容
		 * @param	param
		 * @return Object
		 */
		public function getFile(param:String):Object {
			if (file != null) {
				var _temp:Object = file[param];
				delete file[param];
				return _temp;
			}
			return null;
		}
		/**
		 * 刪除文件緩存
		 * @param	param
		 */
		public function destroyFileTemp(param:String = ''):void {			
			if ( param == '' ) {
				file = null;
				loaderArr = [];
				//delete file;
			}else {
				file[param] = null;
				delete file[param];
			}

		}
	}

}
